package com.steel.yfmall.cloud.assistant.listener;

import com.alibaba.fastjson.JSON;
import com.steel.yfmall.cloud.assistant.goods.ProductChannelGoodsPriceService;
import com.steel.yfmall.cloud.assistant.goods.ProductSellerGoodsService;
import com.steel.yfmall.cloud.assistant.dto.para.goods.ParaProductChannelGoodsPriceBean;
import com.steel.yfmall.cloud.assistant.dto.para.goods.ParaProductSellerGoodsBean;
import com.steel.yfmall.cloud.assistant.po.goods.ProductChannelGoodsPrice;
import com.steel.yfmall.cloud.assistant.po.goods.ProductSellerGoods;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
public class ProductPriceUpdateListener {

    @Resource
    private ProductSellerGoodsService productSellerGoodsService;

    @Resource
    private ProductChannelGoodsPriceService productChannelGoodsPriceService;

    /**
     * 同步价格后刷新商品基础价格
     *
     * @return void    返回类型
     * @Title: RefreshProductTypePriceListenerOnMessage
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param onMsgText 参数说明
     * @author yuchaohua
     * @date 2020/12/8 15:17
     */
    @JmsListener(destination = "template_update_product_price")
    public void TemplateUpdateProductPriceListenerOnMessage(String onMsgText) {
        try {
            if (log.isDebugEnabled()) {
                log.info("template_update_product_price" + onMsgText);
            }
            if (StringUtils.isBlank(onMsgText)) {
                log.error("同步价格后刷新商品基础价格:参数为空");
                return;
            }
            ParaProductSellerGoodsBean sellerGoodsBean = JSON.parseObject(onMsgText, ParaProductSellerGoodsBean.class);
            if (sellerGoodsBean == null || (StringUtils.isBlank(sellerGoodsBean.getProductSysCode()) && CollectionUtils.isEmpty(sellerGoodsBean.getProductSysCodeList()))
                    || sellerGoodsBean.getMarketPrice() == null) {
                log.error("同步价格后刷新商品基础价格:参数为空");
                return;
            }
            ProductSellerGoods recordBean = new ProductSellerGoods();
            recordBean.setMarketPrice(sellerGoodsBean.getMarketPrice());
            recordBean.setUpdateUser("同步价格信息");
            recordBean.setControllerName("TemplateUpdateProductPriceListener");
            // 如果价格改成功了 修改channelPrice表中的价格
            if (productSellerGoodsService.updateProductSellerGoods(sellerGoodsBean,recordBean)){
                // 修改规格表价格
                ParaProductChannelGoodsPriceBean paraBean = new ParaProductChannelGoodsPriceBean();
                paraBean.setProductSysCode(sellerGoodsBean.getProductSysCode());
                paraBean.setProductSysCodeList(sellerGoodsBean.getProductSysCodeList());
                ProductChannelGoodsPrice channelGoodsPrice = new ProductChannelGoodsPrice();
                channelGoodsPrice.setSpecPrice(sellerGoodsBean.getMarketPrice());
                channelGoodsPrice.setMemberPrice(sellerGoodsBean.getMarketPrice());
                channelGoodsPrice.setUpdateUser("同步价格信息");
                channelGoodsPrice.setControllerName("TemplateUpdateProductPriceListener");
                productChannelGoodsPriceService.updateProductChannelGoodsPrice(paraBean,channelGoodsPrice);
            }
        } catch (Exception e) {
            log.error(onMsgText + "同步价格后刷新商品基础价格失败", e);
        }
    }


}
